﻿/******************************************************************************
 * Author:      671643387
 * Created:     2016/10/25
 *****************************************************************************/

using System.Collections;

/// <summary>
/// 登录请求数据
/// </summary>
public class LoginReqInfo : BaseDomain
{
    ///////////////////////////////////////////////////////////////////////////
    //>> 变量

    /// <summary>
    /// 平台编号
    /// </summary>
    private string _openid;

    /// <summary>
    /// 玩家名
    /// </summary>
    private string _name;

    /// <summary>
    /// 头像地址
    /// </summary>
    private string _headpic;

    /// <summary>
    /// 性别
    /// </summary>
    private int _sex;

    /// <summary>
    /// 过期时间
    /// </summary>
    private long _timeout;


    ///////////////////////////////////////////////////////////////////////////
    //>> 函数

    /// <summary>
    /// 构造函数
    /// </summary>
    public LoginReqInfo()
    {
        _openid = null; _name = null;  _headpic = null; _sex = 0;
    }

    public string openid
    {
        get { return _openid; }
        set { _openid = value; }
    }

    public string name
    {
        get { return _name; }
        set { _name = value; }
    }

    public string headpic
    {
        get { return _headpic; }
        set { _headpic = value; }
    }

    public int sex
    {
        get { return _sex; }
        set { _sex = value; }
    }

    public long timeout
    {
        get { return _timeout; }
        set { _timeout = value; }
    }

    protected override void FillMessage(Hashtable data)
    {
        data["openid"] = _openid;
        data["name"] = _name;
        data["headpic"] = _headpic;
        data["sex"] = _sex;
    }

    public string ToJSON()
    {
        Hashtable data = new Hashtable();
        data["openid"] = _openid;
        data["name"] = _name;
        data["headpic"] = _headpic;
        data["sex"] = _sex;
        data["timeout"] = _timeout;
        return MiniJSON.JsonEncode(data);
    }

    public static LoginReqInfo FromJson(string json)
    {
        LoginReqInfo reqInfo = new LoginReqInfo();
        HashtableEx data = new HashtableEx((Hashtable)MiniJSON.JsonDecode(json));
        reqInfo._openid = data.GetString("openid");
        reqInfo._name = data.GetString("name");
        reqInfo._headpic = data.GetString("headpic");
        reqInfo._sex = data.GetInt("sex");
        reqInfo._timeout = data.GetInt("timeout");
        return reqInfo;
    }

    public static bool HasCache()
    {
        return LocalStorage.HasKey("平台登录数据");
    }

    public static LoginReqInfo FromCache()
    {
        return FromJson(LocalStorage.GetString("平台登录数据"));
    }

    public void SetCache()
    {
        LocalStorage.SetString("平台登录数据", ToJSON());
    }

    public static void ClearCache()
    {
        LocalStorage.ClearKey("平台登录数据");
    }

    public bool valid
    {
        get
        {
            return (_openid != null) && (_openid.Length > 0);
        }
    }
}
